package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/10/24 0:17
 * @description：
 */
public class 用栈实现队列 {
    @Test
    public void test() {
    }

    class MyQueue {

        private Stack<Integer> A, B;
        public MyQueue() {
            A = new Stack<>();
            B = new Stack<>();
        }

        public void push(int x) {
            A.push(x);
        }

        public int pop() {
            if (A.isEmpty() && B.isEmpty()) return -1;
            if (!B.isEmpty()) {
                return B.pop();
            } else {
                while (!A.isEmpty()) {
                    B.push(A.pop());
                }
                return pop();
            }
        }

        public int peek() {
            if (A.isEmpty() && B.isEmpty()) return -1;
            if (!B.isEmpty()) {
                return B.peek();
            } else {
                while (!A.isEmpty()) {
                    B.push(A.pop());
                }
                return peek();
            }
        }

        public boolean empty() {
            return A.isEmpty() && B.isEmpty();
        }
    }

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
}
